package com.zyh.scs.admin.utils.jd;

import com.zyh.scs.admin.api.dto.jd.PayConfirmParam;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * desc: 参数加密解密
 *
 * @author hwh
 */
public class PayConfirmParamUtil {

    /**
     * 参数加密
     *
     * @param srcStr 要加密的原始字符串
     * @param rsaEncryptPubKey 加密RSA公钥
     * @return
     */
    public static String encryptParam(String srcStr, String rsaEncryptPubKey) {
        try {
            byte[] s1Byte = RsaCoder.encryptByPublicKey(srcStr.getBytes(),rsaEncryptPubKey);
            String s1 = RsaCoder.encryptBASE64(s1Byte);
            return s1;
        } catch (Exception e) {
            System.out.println("参数加密失败,srcStr=" + srcStr);
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 参数解密
     * @param encryptedStr 已加密的字符串
     * @param rsaSignPrivKey 加密RSA私钥
     * @return
     */
    public static String decryptParam(String encryptedStr, String rsaSignPrivKey) {
        try {
            byte[] s1EncryptedByte = RsaCoder.decryptBASE64(encryptedStr);
            byte[] s1Byte = RsaCoder.decryptByPrivateKey(s1EncryptedByte, rsaSignPrivKey);
            return new String(s1Byte);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    public static PayConfirmParam getPayConfirmParam(String rsaSignPrivateKey, String rsaEncryptPublicKey, String accessKey, String jdOrderId) throws Exception {

    	PayConfirmParam payConfirmParam = new PayConfirmParam();

    	payConfirmParam.setAccessKey(accessKey);
    	payConfirmParam.setJdOrderId(jdOrderId);
    	
        // Long timestamp = System.currentTimeMillis() - 15;
        Long timestamp = System.currentTimeMillis() / 1000 - 18;
        payConfirmParam.setTimestamp(timestamp + "");
        // autoLoginParam.setTimestamp(timestamp + "");
        // autoLoginParam.setTimestamp(DateUtil.offsetMinute(new Date(), 3).getTime() + "");

        //sign验证 //这两个参数不参与签名
        List<String> unsignedKeyList = new ArrayList<>();
        unsignedKeyList.add("version");   
        unsignedKeyList.add("sign");

        // 获取验签sign
        // 1.将参数按照ASCII码从小到大的顺序排列并使用&符号拼接-s1
        String s1 = SignatureUtil.signString(payConfirmParam, unsignedKeyList);
        // 2.将s1使用"SHA-256"加密方式进行加密获取字符串-s2
        String s2 = ShaUtil.encrypt(s1, "SHA-256");
        // 3.将s2再使用验签私钥进行加密成为s3
        assert s2 != null;
        byte[] s2Rsa = RsaCoder.encryptByPrivateKey(s2.getBytes(StandardCharsets.UTF_8), rsaSignPrivateKey);
        // 4.将s3进行base64进行编码
        String sign = RsaCoder.encryptBASE64(s2Rsa);

        // 签名参数
        //payConfirmParam.setAccessKey(PayConfirmParamUtil.encryptParam(payConfirmParam.getAccessKey(),rsaEncryptPublicKey));
        //payConfirmParam.setJdOrderId(PayConfirmParamUtil.encryptParam(payConfirmParam.getJdOrderId(),rsaEncryptPublicKey));
        //payConfirmParam.setTimestamp(PayConfirmParamUtil.encryptParam(payConfirmParam.getTimestamp(),rsaEncryptPublicKey));
        payConfirmParam.setSign(sign);

        return payConfirmParam;
    }

    private static boolean isNotBlank(String s){
        if(s != null && !s.isEmpty() && !s.trim().isEmpty()){
            return true;
        }
        return false;
    }
}
